//
//  TORadioHeader.m
//  B段项目
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TORadioHeader.h"

@interface TORadioHeader ()<SDCycleScrollViewDelegate>

@property(nonatomic,strong)UILabel *allRAdio;
@property(nonatomic,strong)UIView *lineView;

@property(nonatomic,strong) NSMutableArray *imageArray;
@property(nonatomic,strong) NSMutableArray *urlArr;

@end

@implementation TORadioHeader


-(instancetype)init
{
    self = [super init];
    
    if (self) {
        [self createSubview];
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubview];
    }
    
    return self;
}

-(void)createSubview
{
    self.lunScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/4)];
    self.lunScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.lunScrollView];
    self.lunScrollView.delegate = self;
    
    self.allRAdio = [[UILabel alloc] init];
    self.allRAdio.text = @"全部电台 · All Radios";
    self.allRAdio.textColor = [UIColor lightGrayColor];
    self.allRAdio.font = [UIFont systemFontOfSize:13 weight:0];
    [self addSubview:self.allRAdio];
    [self.allRAdio mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(20);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(self).multipliedBy(0.35);
        make.height.mas_equalTo(20);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.allRAdio.mas_right).offset(5);
        make.right.mas_equalTo(self);
        make.centerY.mas_equalTo(self.allRAdio);
        make.height.mas_equalTo(0.5);
        
    }];
    
    
    
}

-(void)setHeaderArray:(NSMutableArray *)headerArray ShowArray:(NSMutableArray *)showArray
{
    self.urlArr = [NSMutableArray array];
    NSMutableArray *imageUrlArr = [NSMutableArray array];
    for (NSDictionary *dic in showArray) {
        NSURL *url = [dic objectForKey:@"img"];
        [imageUrlArr addObject:url];
        NSString *str = [dic objectForKey:@"url"];
//        NSLog(@"%@", str);
        str = [str substringWithRange:NSMakeRange(12, 24)];
//        NSLog(@"%@", str);
        [self.urlArr addObject:str];
    }
    self.lunScrollView.imageURLStringsGroup = imageUrlArr;
    self.lunScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    
    for (int i = 0; i < 3; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + ((self.width - 30)/3 + 5) * i, self.lunScrollView.origin.y + self.lunScrollView.frame.size.height + 10, (self.width - 30)/3 , (self.width - 30)/3)];
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.tag = 1000 + i;
        
        TORadioModel *model = [headerArray objectAtIndex:i];
        [imageView sd_setImageWithURL:model.coverimg placeholderImage:PLACEHOLDER_IMAGE];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        
        [self addSubview:imageView];
        
        
    }
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    [self.radioHeaderDelegate jumpToNextPage:tap.view.tag-1000];
}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
//    NSLog(@"%@", self.urlArr[index]) ;
    [self.radioHeaderDelegate presentToDetailVCByRadioID:self.urlArr[index]];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
